﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GameJamSnake
{
    class Level
    {
        private Tile[,] tiles;

        public Level()
        {

        }

        public void Draw(SpriteBatch sbatch)
        {
            foreach(Tile t in tiles)
            {
                t.Draw(sbatch);
            }
        }

        public void LoadLevel(Texture2D lvlImage, Tile[,] grid)
        {
            Color[] pixels = new Color[lvlImage.Height * lvlImage.Width];
            lvlImage.GetData<Color>(pixels);

            for (int i = 0; i < lvlImage.Height * lvlImage.Width; ++i)
            {
                int x = i % lvlImage.Width;
                int y = i / lvlImage.Width;

                Tile tempTile = new Tile(x, y);
                tempTile.Load(TextureManager.Instance.GetTex("DeadSnake"), TextureManager.Instance.GetTex("Stone"), TextureManager.Instance.GetTex("Power"), TextureManager.Instance.GetTex("Clock"));

                if (pixels[i] == Color.White)
                {
                    tempTile.EmptyTile();
                }

                if (pixels[i] == Color.Black)
                {
                    tempTile.Petrify();
                }

                grid[x, y] = tempTile;
            }
        }

        public void Petrify(int X,int Y)
        {
            tiles[X, Y].Petrify();
        }

        public Tile.TileState GetTileState(int X,int Y)
        {
            return tiles[X, Y].GetState();
        }

        public void PowerUp(int X,int Y)
        {
            tiles[X, Y].PowerUp();
        }

        public void SetEmptyTile(int X, int Y)
        {
            tiles[X, Y].EmptyTile();
        }
    }
}
